package com.huike.clues.service.impl;

import java.util.Date;
import java.util.List;

import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.service.ITbClueService;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.service.ITbClueTrackRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 线索跟进记录Service业务层处理
 * @date 2021-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService 
{
    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;
    @Autowired
    private ITbClueService clueService;

    /**
     * 查询线索跟进记录列表
     * 
     * @param tbClueTrackRecord 线索跟进记录
     * @return 线索跟进记录
     */
    @Override
    public List<TbClueTrackRecord> selectTbClueTrackRecordList(TbClueTrackRecord tbClueTrackRecord)
    {
        return tbClueTrackRecordMapper.selectTbClueTrackRecordList(tbClueTrackRecord);
    }

    /**
     * 修改线索跟进记录
     * 
     * @param tbClueTrackRecord 线索跟进记录
     * @return 结果
     */
    @Override
    public int updateTbClueTrackRecord(TbClueTrackRecord tbClueTrackRecord)
    {
        return tbClueTrackRecordMapper.updateTbClueTrackRecord(tbClueTrackRecord);
    }

    /**
     * 批量删除线索跟进记录
     * 
     * @param ids 需要删除的线索跟进记录ID
     * @return 结果
     */
    @Override
    public int deleteTbClueTrackRecordByIds(Long[] ids)
    {
        return tbClueTrackRecordMapper.deleteTbClueTrackRecordByIds(ids);
    }

    /**
     * 删除线索跟进记录信息
     * 
     * @param id 线索跟进记录ID
     * @return 结果
     */
    @Override
    public int deleteTbClueTrackRecordById(Long id)
    {
        return tbClueTrackRecordMapper.deleteTbClueTrackRecordById(id);
    }

    @Override
    @Transactional
    public int insertTbClueTrackRecord(ClueTrackRecordVo clueTrackRecordVo) {
        Date now = DateUtils.getNowDate();
        TbClue clue = new TbClue();
        clue.setId(clueTrackRecordVo.getClueId());
        clue.setLevel(clueTrackRecordVo.getLevel());
        clue.setUpdateBy(SecurityUtils.getUsername());
        clue.setUpdateTime(now);
        clue.setSubject(clueTrackRecordVo.getSubject());
        clue.setName(clueTrackRecordVo.getName());
        clue.setSex(clueTrackRecordVo.getSex());
        clue.setWeixin(clueTrackRecordVo.getWeixin());
        clue.setQq(clueTrackRecordVo.getQq());
        clue.setAge(clueTrackRecordVo.getAge());
        int i = clueService.updateTbClue(clue);
        if(i > 0){
            TbClueTrackRecord clueTrackRecord = new TbClueTrackRecord();
            clueTrackRecord.setCreateBy(SecurityUtils.getUsername());

            clueTrackRecord.setCreateTime(now);
            clueTrackRecord.setClueId(clueTrackRecordVo.getClueId());
            clueTrackRecord.setRecord(clueTrackRecord.getRecord());
            clueTrackRecord.setLevel(clueTrackRecordVo.getLevel());
            clueTrackRecord.setSubject(clueTrackRecordVo.getSubject());
            clueTrackRecord.setRecord(clueTrackRecordVo.getRecord());
            clueTrackRecord.setFalseReason(clueTrackRecordVo.getFalseReason());
            clueTrackRecord.setNextTime(clueTrackRecordVo.getNextTime());

            return tbClueTrackRecordMapper.insertTbClueTrackRecord(clueTrackRecord);
        }
        return 0;


    }

    @Override
    public int insertTbClueTrackRecord(TbClueTrackRecord clueTrackRecord) {
        clueTrackRecord.setCreateBy(SecurityUtils.getUsername());
        clueTrackRecord.setCreateTime(DateUtils.getNowDate());
        return tbClueTrackRecordMapper.insertTbClueTrackRecord(clueTrackRecord);
    }

}
